﻿Option Strict On

Public Class Players
    Private PlayerListDelim As String = "|"
    Private PlayerStatsDelim As String = "/"
    Private pvPlayerListofStrings As New List(Of String)
    Private pvPlayerListofNames As New List(Of String)
    Private pvPlayerListofIds As New List(Of Integer)
    Private pvPlayerIndex As Integer
    Private NextId As Integer

    'player data for active player
    Private pvPlayerString As String

    'player stats for each player
    Private pvPlayerId As Integer
    Private pvPlayerName As String
    Private pvTotalGames As Integer = 0
    Private pvTotalWins As Integer = 0
    Private pvTotalTime As Single = 0
    Private pvMinTime As Single = 0
    Private pvMaxTime As Single = 0

    Public Sub New()

    End Sub



    Public ReadOnly Property TotalGames() As Integer
        Get
            Return pvTotalGames
        End Get
    End Property

    Public ReadOnly Property TotalWins() As Integer
        Get
            Return pvTotalWins
        End Get
    End Property

    Public ReadOnly Property AverageWins() As Single
        Get
            Return CSng(pvTotalWins) / CSng(pvTotalGames)
        End Get
    End Property

    Public ReadOnly Property AverageTime() As Single
        Get
            If pvTotalGames = 0 Then
                Return 0
            Else
                Return CSng(pvTotalTime) / CSng(pvTotalGames)
            End If
        End Get
    End Property

    Public ReadOnly Property FastestTime() As Single
        Get
            Return CSng(pvMinTime)
        End Get
    End Property


    Public ReadOnly Property PlayerName() As String
        Get
            Return pvPlayerName
        End Get
    End Property

    Public ReadOnly Property PlayerId() As Integer
        Get
            Return pvPlayerId
        End Get
    End Property


    ''' <summary>
    ''' load string from settings containing data for all players (id, name, stats, etc.)
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub LoadSettings(ByVal PlayerData As String, ByVal LastPlayerId As Integer)
        Dim player() As String
        Dim PlayerId As Integer

        'default stats
        If 1 = 1 Or PlayerData = "" Then
            PlayerData = "1/Anonymous/10/6/245/21.50/29.0|2/Todd/24/21/578.4/19.05/25.0|3/Marci/12/9/302.4/24.44/26.2|4/Mike/3/1/84.1/24.00/28.6"
            LastPlayerId = 1
        End If

        NextId = 0

        'split  data into array then store as a list
        pvPlayerListofStrings.Clear()
        pvPlayerListofStrings.AddRange(Split(PlayerData, PlayerListDelim))

        'remove any empty strings from list
        While pvPlayerListofStrings.Contains("")
            pvPlayerListofStrings.Remove("")
        End While

        pvPlayerListofNames.Clear()
        pvPlayerListofIds.Clear()

        For Each item As String In pvPlayerListofStrings
            Debug.Print(item)
            player = Split(item, PlayerStatsDelim)
            PlayerId = CInt(player(0))
            pvPlayerListofIds.Add(PlayerId)
            pvPlayerListofNames.Add(CStr(player(1)))

            'increment max id
            If PlayerId > NextId Then NextId = PlayerId

            'assign active player
            If PlayerId = LastPlayerId Then
                pvPlayerString = item
                Call ParsePlayerString()
            End If
            'Stop
        Next

        NextId = NextId + 1
        'Stop
    End Sub


    ''' <summary>
    ''' return list of player names
    ''' </summary>
    ''' <remarks></remarks>
    Public Function GetPlayerNames() As List(Of String)
        Return pvPlayerListofNames
    End Function



    ''' <summary>
    ''' update player stats with game results
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub AddGame(ByVal GameTime As Single, ByVal LimitTime As Single)
        pvTotalGames = pvTotalGames + 1
        If GameTime < LimitTime Then pvTotalWins = pvTotalWins + 1
        pvTotalTime = pvTotalTime + GameTime
        If GameTime < pvMinTime Then pvMinTime = GameTime
        If GameTime > pvMaxTime Then pvMaxTime = GameTime

        'Stop
        Call BuildPlayerString()

    End Sub


    ''' <summary>
    ''' update player stats with game results
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ClearStats()
        pvTotalGames = 0
        pvTotalWins = 0
        pvTotalTime = 0.0
        pvMinTime = 0.0
        pvMaxTime = 0.0
        Call BuildPlayerString()
    End Sub


    ''' <summary>
    ''' add new player name to list of players
    ''' </summary>
    ''' <remarks></remarks>
    Public Function AddPlayer(ByVal PlayerName As String) As String
        AddPlayer = ""
        Dim val As Boolean = pvPlayerListofNames.Contains(PlayerName, StringComparer.OrdinalIgnoreCase)

        If Not val Then
            pvPlayerId = NextId
            pvPlayerName = PlayerName
            Call ClearStats()
            Call BuildPlayerString()
            pvPlayerListofStrings.Add(pvPlayerString)
            pvPlayerListofNames.Add(PlayerName)
            pvPlayerIndex = pvPlayerListofStrings.Count - 1
            NextId = NextId + 1
        Else
            AddPlayer = pvPlayerName & " already exists."
        End If
        'Stop

    End Function


    ''' <summary>
    ''' delete active player name from list of players
    ''' </summary>
    ''' <remarks></remarks>
    Public Function DeletePlayer() As String
        DeletePlayer = ""

        If pvPlayerId = 1 Then
            'Player 1 is the default player, there must be at least one player
            DeletePlayer = "Cannot remove player " & pvPlayerName & ". It is the default player."
        ElseIf pvPlayerListofNames.Count = 1 Then
            'this shouldn't ever occur if the above is handled properly but just in case
            DeletePlayer = "Cannot remove only player."
        Else
            'Dim intX As Integer = pvPlayerListofNames.IndexOf(pvPlayerName)
            pvPlayerListofIds.Remove(pvPlayerId)
            pvPlayerListofNames.Remove(pvPlayerName)
            pvPlayerListofStrings.Remove(pvPlayerString)
            pvPlayerIndex = pvPlayerIndex - 1
            If pvPlayerIndex < 0 Then pvPlayerIndex = 0
            Call SetPlayer(pvPlayerListofNames(pvPlayerIndex))
        End If
        'Stop

    End Function


    ''' <summary>
    ''' rename active player, return "" if ok, else error message
    ''' </summary>
    ''' <remarks></remarks>
    Public Function RenamePlayer(ByVal PlayerName As String) As String
        RenamePlayer = ""
        
        'already this name, exact
        If pvPlayerName Is PlayerName Then Exit Function

        'don't rename default player
        If pvPlayerId = 1 Then
            'Player 1 is the default player
            RenamePlayer = "Cannot rename player " & pvPlayerName & ". It is the default player."
            Exit Function
        End If

        'this name already in the list, different case
        For Each item As String In GetPlayerNames()
            If item.ToUpper = PlayerName.ToUpper AndAlso item.ToUpper <> pvPlayerName.ToUpper Then
                RenamePlayer = "Player name already exists."
                Exit Function
            End If
        Next

        'rename
        pvPlayerName = PlayerName
        Call BuildPlayerString()
        pvPlayerListofNames(pvPlayerIndex) = pvPlayerName
        pvPlayerListofStrings(pvPlayerIndex) = pvPlayerString
        'stop

    End Function


    ''' <summary>
    ''' set active player
    ''' </summary>
    ''' <remarks></remarks>
    Public Overloads Sub SetPlayer(ByVal PlayerName As String)
        Dim intX As Integer = pvPlayerListofNames.IndexOf(PlayerName)
        If intX >= 0 Then
            Debug.Print(pvPlayerListofStrings(intX))
            pvPlayerString = pvPlayerListofStrings(intX)
            Call ParsePlayerString()
        End If
    End Sub
    Public Overloads Sub SetPlayer(ByVal PlayerId As Integer)
        Dim intX As Integer = pvPlayerListofIds.IndexOf(PlayerId)
        If intX >= 0 Then
            Debug.Print(pvPlayerListofStrings(intX))
            pvPlayerString = pvPlayerListofStrings(intX)
            Call ParsePlayerString()
        End If
    End Sub


    ''' <summary>
    ''' set player parameters from player data string
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ParsePlayerString()
        Dim player() As String

        'assign index for active player
        pvPlayerIndex = pvPlayerListofStrings.IndexOf(pvPlayerString)

        'assign stats for active player
        player = Split(pvPlayerString, PlayerStatsDelim)
        pvPlayerId = CInt(player(0))
        pvPlayerName = CStr(player(1))
        pvTotalGames = CInt(player(2))
        pvTotalWins = CInt(player(3))
        pvTotalTime = CInt(player(4))
        pvMinTime = CSng(player(5))
        pvMaxTime = CSng(player(6))

    End Sub



    ''' <summary>
    ''' build single player data string from player parameters
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BuildPlayerString()
        Dim player() As String = {CStr(pvPlayerId), CStr(pvPlayerName), CStr(pvTotalGames), CStr(pvTotalWins), CStr(pvTotalTime), CStr(pvMinTime), CStr(pvMaxTime)}
        pvPlayerString = String.Join(PlayerStatsDelim, player.ToArray())
        pvPlayerListofStrings(pvPlayerIndex) = pvPlayerString
    End Sub


    ''' <summary>
    ''' build all players data string from player parameters
    ''' </summary>
    ''' <remarks></remarks>
    Public Function BuildSettings() As String
        BuildSettings = String.Join(PlayerListDelim, pvPlayerListofStrings.ToArray())
    End Function


End Class
